import {
  Component,
  EventEmitter,
  Injector,
  Input,
  OnInit,
  Output,
} from '@angular/core';
import {
  ColumnInfoServiceProxy,
  GetDataComboItemDtoOfInt64,
} from '@abp/service-proxies/service-proxies';
import { AppComponentBase } from '@abp/components';

@Component({
  selector: 'cloumn-parent-combo',
  templateUrl: './cloumn-parent-combo.component.html',
  styles: [],
})
export class CloumnParentComboComponent extends AppComponentBase
  implements OnInit {
  cloumns: GetDataComboItemDtoOfInt64[] = [];
  @Input() placeholder: string;
  @Input() selectedCloumn: GetDataComboItemDtoOfInt64 = undefined;
  @Output() selectedCloumnChange: EventEmitter<
    GetDataComboItemDtoOfInt64
    > = new EventEmitter<GetDataComboItemDtoOfInt64>();
  @Input() isFindRoot: boolean;

  private _cloumnId: number;
  @Input() cloumnId: number;

  constructor(
    private _columnService: ColumnInfoServiceProxy,
    injector: Injector,
  ) {
    super(injector);
  }

  ngOnChanges(): void {
    this._columnService
      .getFirstLevelComboItems(this.isFindRoot)
      .subscribe(result => {
        this.cloumns = result;
        this._cloumnId = this.cloumnId;
        this.autoSelect();
      });

  }

  ngOnInit(): void { }

  autoSelect() {
    if (!this.cloumns || !this._cloumnId) {
      return;
    }
    this.selectedCloumn = this.cloumns.find(
      item => item.value == this._cloumnId,
    );
    if (this.selectedCloumnChange) {
      this.selectedCloumnChange.emit(this.selectedCloumn);
    }
  }
}
